<template>
  <div class="com-container">
    <div class="com-chart" ref="sellerRef"></div>
  </div>
</template>


<script>
import "../../public/static/theme/chalk";
import { mapState } from "vuex";

export default {
  data() {
    return {
      myEchart: null,
      allDataList: [], //获取的数据，包括name和value
      currentPage: 1, //当前的页数
      totalPage: 0, //总页数
      timer: "",
    };
  },
  created() {
    this.$socket.registerCallBack("sellerData", this.getData);
  },
  mounted() {
    this.initChart();
    // this.getData();
    this.$socket.send({
      action: "getData",
      socketType: "sellerData",
      chartName: "seller",
      value: "",
    });
    //在页面加载完成时，主动对屏幕进行适配
    this.screenAdapter();
    window.addEventListener("resize", this.screenAdapter);
  },

  destroyed() {
    clearInterval(this.timer);
    window.removeEventListener("resize", this.screenAdapter);
    this.$socket.unRegisterCallBack("sellerData");
  },
  computed: {
    ...mapState(["theme"]),
  },
  watch: {
    theme() {
      //销毁当前图表
      this.myEchart.dispose();
      //以最新的主题初始化图表对象
      this.initChart();
      //屏幕适配
      this.screenAdapter();
      //渲染数据
      this.updateChart();
    },
  },
  methods: {
    //====================初始化图表=======================
    initChart() {
      this.myEchart = this.$echarts.init(this.$refs.sellerRef, this.theme);
      const initOption = {
        title: {
          text: "▎商家销售统计",
          top: 15,
          left: 15,
        },
        grid: {
          top: "20%",
          left: "3%",
          right: "6%",
          bottom: "3%",
          containLabel: true,
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "line",
            z: 0,
            lineStyle: {
              width: 50,
              color: "#2D3443",
            },
          },
        },
        xAxis: {
          type: "value",
        },
        yAxis: {
          type: "category",
        },
        series: [
          {
            type: "bar",
            barWidth: 50,
            label: {
              show: true,
              position: "right",
              textStyle: {
                color: "white",
              },
            },
            itemStyle: {
              barBorderRadius: [0, 25, 25, 0],
              // color:new this.$echarts.grahic.LinearGradient(x1,y1,x2,y2,[])
              color: new this.$echarts.graphic.LinearGradient(0, 1, 1, 0, [
                {
                  //百分之0状态下的颜色值
                  offset: 0,
                  color: "#5052EE",
                },
                {
                  //百分之100状态下的颜色值
                  offset: 1,
                  color: "#AB6EE5",
                },
              ]),
            },
          },
        ],
      };
      this.myEchart.setOption(initOption);
      this.myEchart.on("mouseover", () => {
        clearInterval(this.timer);
      });
      this.myEchart.on("mouseout", () => {
        this.startInterval();
      });
    },
    //====================请求数据=========================
    getData(res) {
      // const { data: res } = await this.$http.get("seller");
      //   console.log(res);
      //先将获取过来的数据根据value大小进行排序
      res.sort((a, b) => {
        return a.value - b.value;
      });
      // console.log(res);
      this.allDataList = res;
      //总页数 向上取整
      this.totalPage = Math.ceil(res.length / 5);
      this.updateChart();
      this.startInterval();
    },
    //========================更新数据======================
    updateChart() {
      const start = (this.currentPage - 1) * 5; //当前页的起始数据
      const end = this.currentPage * 5; //当前页的结束数据
      const showData = this.allDataList.slice(start, end);
      //将名字和数据存到两个数组中
      const sellerName = showData.map((item) => item.name);
      const sellerValue = showData.map((item) => item.value);
      //准备数据源
      const options = {
        yAxis: {
          data: sellerName,
        },
        series: [
          {
            data: sellerValue,
          },
        ],
      };
      this.myEchart.setOption(options);
    },

    //============设置定时器 每隔3秒分页显示数据===============
    startInterval() {
      if (this.timer) {
        clearInterval(this.timer);
      }
      this.timer = setInterval(() => {
        this.currentPage++;
        if (this.currentPage > this.totalPage) this.currentPage = 1;
        this.updateChart();
      }, 3000);
    },

    //==================屏幕适配============================
    screenAdapter() {
      const titleFontSize = (this.$refs.sellerRef.offsetWidth / 100) * 4;
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize,
          },
        },
        tooltip: {
          axisPointer: {
            lineStyle: {
              width: titleFontSize,
            },
          },
        },
        series: [
          {
            barWidth: titleFontSize,
            itemStyle: {
              barBorderRadius: [0, titleFontSize / 2, titleFontSize / 2, 0],
            },
          },
        ],
      };
      this.myEchart.setOption(adapterOption);
      this.myEchart.resize();
    },
  },
};
</script>

